---博康卡口,上传类型,ftp


local utils                    = require "gcs.utils"
local cjson                    = require "cjson"
local db_method                = require "gcs.db.method"
local config_table = ngx.shared.gcs_config   --声明ngx.shared.gcs_config
local ipshine_table = ngx.shared.gcs_ipshine   --声明ngx.shared.gcs_ipshine
local form_timeout = config_table:get("p_form_timeout")
local form_chunk_size = config_table:get("p_form_chunk_size")
local mongo_table_name = "gcxx"
local format = "jpg"
local cjson_group = config_table:get("p_group_table")
local store_connect_timeout = config_table:get("p_store_connect_timeout")
local store_keep_timeout = config_table:get("p_store_keep_timeout")
local store_keep_count = config_table:get("p_store_keep_count")
local remote_ip                = ngx.var.remote_addr              --获得客户端ip
--utils.gcs_log("error","remote_ip:" ..remote_ip)
-----------校验参数
local tb_parameters = {}
tb_parameters.form_timeout = form_timeout
tb_parameters.form_chunk_size = form_chunk_size
tb_parameters.store_connect_timeout = store_connect_timeout
tb_parameters.store_keep_timeout = store_keep_timeout
tb_parameters.store_keep_count = store_keep_count
tb_parameters.cjson_group = cjson_group

local check_parameters = {
  "store_keep_timeout",                 "store_keep_count",                "cjson_group"
}

local res,err,error_no = utils.check_parameter(tb_parameters,check_parameters)
if not res then
  utils.gcs_log("error","second_line upload,check_parameter is err,err:" .. cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1001
  local message = err
  return ngx.say(cjson.encode(tb_return))
end

--------------------------加载函数---------------------

local red_method               = require "gcs.list.method"
local mongo_method             = require "gcs.mongodb.method"
local storage                  = require "resty.gcsdfs.storage"              --加载storage �?
local multipart                = require "resty.gcsdfs.multipart"
local up_utils                 = require "gcs.upload.upload_utils"
local up_hex                   = require "gcs.upload.hex"
local db_method                = require "gcs.db.method"
utils.gcs_log("debug","second_line upload,require complete")


---------------------------拼装信息----------------------------
---定义需要插入的字段放入table�?
--local lkbh,time,Hphm,hpys,xssd,xsfx,cd,wfxwdm,hmsztpzb,csys,clpp,sbbh,tpmc
--local gfile,filename,vssid
local db_data = {}
---获取�?
local form = multipart:new(form_timeout,form_chunk_size)
local res,err = form:parse()
if not res then
  ngx.req.discard_body()
  utils.gcs_log("error","second_line upload,form parse is nil,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1002
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
local form_res,err = form.fields
if not form_res then
  ngx.req.discard_body()
  utils.gcs_log("error","second_line upload,form_res is nil,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1003
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
---校验form_res中的??�段是否存�?
local res,err = up_utils.check_form_res(form_res)
if not res then
  ngx.req.discard_body()
  utils.gcs_log("error","second_line upload,check_form_res is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1004
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
utils.gcs_log("debug","second_line upload,check form_res is ok")

local db_data = {}

----------取上传来的filename
local filename = form_res.gfile1.filename
filename = string.sub(filename,1,-5)
--解析fname
local rt,err = up_utils.StringSplit(filename,"-")
if not rt then
  local tb_return = {}
  local vssid = 0
  local code = 1005
  local message = err
  return ngx.say(cjson.encode(tb_return))
end

--0272012LF12164-20140810160617933-无牌-4-061-E-1-00000-0,0,0,0-蓝色-其他.jpg
db_data.lkbh = rt[1]
db_data.time = rt[2]
db_data.txxh = "000"
db_data.zpzs = 1
db_data.zpxh = 1
db_data.hphm = up_utils.createIconv("gbk","utf-8",rt[3]) or ""           ---转utf8
db_data.hpys = rt[4]
db_data.xssd = rt[5]
db_data.xsfx = rt[6]
db_data.cd = rt[7]
db_data.wfxwdm = rt[8]
db_data.hpsztpzb = rt[9]
db_data.csys = up_utils.createIconv("gbk","utf-8",rt[10])             ---转utf8
db_data.clpp = up_utils.createIconv("gbk","utf-8",rt[11])            ---转utf8
------通过路口编号以及车道获取设备编号


local query = "select * from gcs_ipshine where C_LKBH = "..ngx.quote_sql_str(db_data.lkbh)
query = query .. " and C_CD like '%" .. db_data.cd ..",%;"
local tb_query = db_method.query(query)
utils.gcs_log("error","query: "..query)
if not next(tb_query) then
  utils.gcs_log("error","query: "..query)
  local tb_return = {}
  local vssid = 0
  local code = 1111
  local message = "mysql select is err"
  return ngx.say(cjson.encode(tb_return))
end
db_data.sbbh = tb_query[1].C_SBBH
--0272012LF12164-20140810160617933-无牌-4-061-E-1-00000-0,0,0,0-蓝色-其他.jpg
db_data.tpmc = db_data.lkbh.."-"..db_data.time.."-"..db_data.hphm.."-"..db_data.hpys.."-"..db_data.xssd.."-"..db_data.xsfx..
                "-"..db_data.cd.."-"..db_data.wfxwdm.."-"..db_data.hpsztpzb.."-"..db_data.csys.."-"..db_data.clpp
db_data.filename = filename

db_data.id = db_data.lkbh .."_".. db_data.time.."_" .. db_data.sbbh
db_data.size = form_res.gfile1.file_size
db_data.md5 = form_res.gfile1.md5
--存储storage
local st_res = up_utils.get_storage(cjson_group)
local tb_value = {}
local tb_value = {
  timeout = store_connect_timeout,
  keep_timeout = store_keep_timeout,
  keep_count = store_keep_count
}

local gfile,err = up_utils.save_storage(form_res,st_res,tb_value,format)
if not gfile then
  utils.gcs_log("error","second_line upload,save_storage is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1006
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
db_data.gfile = gfile
db_data.str_res = cjson.encode(st_res)

--------------------------插入数据�?--------------------------------
---插入mongodb,记录数据
local n,err = mongo_method.save_data(mongo_table_name,db_data)
if not n then
  utils.gcs_log("error","second_line upload,save mongodb is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1007
  local message = err
  return ngx.say(cjson.encode(tb_return))
end

---插入redis,key,value
local insert_data = {}
insert_data[db_data.id] = cjson.encode(db_data)
local res,err = red_method.set_data(insert_data)
if not res then
  utils.gcs_log("error","second_line upload,save redis is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1015
  local message = err
  return ngx.say(cjson.encode(tb_return))
end

---插入的队列
local res,err = red_method.push("l","bkkklist",db_data.id)
if not res then
  utils.gcs_log("error","second_line upload,push bkkklist is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1008
  local msg = err
  return ngx.say(cjson.encode(tb_return))
end








